package com.wangxiang.shop.order.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangxiang.common.utils.*;
import com.wangxiang.shop.order.dao.MemberOrderDao;
import com.wangxiang.shop.order.dao.ShipOrderDao;
import com.wangxiang.shop.order.entity.MemberOrderEntity;
import com.wangxiang.shop.order.entity.ShipOrderEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wangxiang.shop.order.dao.ReceiptOrderDao;
import com.wangxiang.shop.order.entity.ReceiptOrderEntity;
import com.wangxiang.shop.order.service.ReceiptOrderService;


@Service("receiptOrderService")
public class ReceiptOrderServiceImpl extends ServiceImpl<ReceiptOrderDao, ReceiptOrderEntity> implements ReceiptOrderService {
    @Autowired
    private ReceiptOrderDao receiptOrderDao;
    @Autowired
    private MemberOrderDao memberOrderDao;
    @Autowired
    private ShipOrderDao shipOrderDao;


    @Override
    public PageEntity page(HashMap<String, Integer> map) {
       PageHelper.startPage(map.get("pageNum"),map.get("pageSize"));
        List<ReceiptOrderEntity> receiptOrderEntities = receiptOrderDao.selectList(null);
        receiptOrderEntities.forEach((item)->{
            TimeUtils timeUtils = new TimeUtils();
            String s = timeUtils.timeChange(item.getTime());
            item.setStrTime(s);
        });
        PageInfo<ReceiptOrderEntity> receiptOrderEntityPageInfo = new PageInfo<>(receiptOrderEntities);

        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(receiptOrderEntityPageInfo);
        return generate;
    }

    @Override
    public List<HashMap<String, Object>> pageList(List<ReceiptOrderEntity> list) {
        ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();

        list.forEach((item)->{
            HashMap<String, Object> map = new HashMap<>();
            MemberOrderEntity entity = memberOrderDao.selectById(item.getOrderId());
            ShipOrderEntity entity1 = shipOrderDao.selectByOrderId(item.getOrderId());
            map.put("orderId",item.getOrderId());
            map.put("userId",entity.getUserId()); //这里的购买人
           //签收时间
            map.put("receiptTime",item.getStrTime());

            TimeUtils timeUtils = new TimeUtils();
            String s = timeUtils.timeChange(entity.getTime());
            if (entity1!=null){
                String s1 = timeUtils.timeChange(entity1.getTime());
                map.put("shipTime",s1);
            }
            map.put("orderTime",s); //下单时间
            map.put("spuId",entity.getSpuId());
            map.put("num",entity.getNum());
            map.put("price",entity.getPrice());
            map.put("skuId",entity.getSkuId());
            map.put("receiptId",entity.getReceiptId());
            hashMaps.add(map);
        });
        return hashMaps;
    }
}